<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('投资主体') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('主体类型')" prop="investorType">
        <dictSelect
          v-model="queryParams.investorType"
          :clearable="true"
          dictionary="deal_investment_type"
        />
      </el-form-item>
      <el-form-item :label="$t('币种')" prop="currency">
        <el-select v-model="queryParams.currency" :placeholder="$t('币种')" clearable size="small">
          <el-option
            v-for="dict in currencyOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('实际投资时间')" prop="dateInvestmentFact">
        <DatePicker v-model="dateInvestmentFacts" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:investment:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:investment:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col>
    </el-row>

    <div v-if="!isEmpty(dealInvestmentList)" v-loading="loading" style="min-height: 555px;">
      <div v-for="(scope, index) in dealInvestmentList" :key="index" class="list-main">
        <div class="list-title-invest">
          <div class="list-title-invest-text">
            <div class="list-title-invest-name">{{ scope.investorName }}</div>
            <el-tag style="border-radius: 15px;height: 20px;line-height: 1.6;margin-right: 10px;">{{ investorTypeFormat(scope) }}</el-tag>
            <el-tag style="border-radius: 15px;height: 20px;line-height: 1.6;margin-right: 10px;">{{ currencyFormat(scope) }}</el-tag>
            <el-tag v-show="scope.investmentRole" style="border-radius: 15px;height: 20px;line-height: 1.6;margin-right: 10px;">{{ roleFormatter(scope) }}</el-tag>
          </div>
          <div style="width: 20%;display: flex;justify-content: flex-end;">
            <el-button
              v-hasPermi="['deal:investment:edit']"
              type="primary"
              size="mini"
              plain
              @click="updateHandle(scope)"
            >{{ $t('编辑') }}</el-button>
            <el-tooltip :disabled="isEmpty(scope.delBtnStatusMsg)" :content="$t('该主体已出资或已在合同中签署协议')" placement="top">
              <el-button
                v-hasPermi="['deal:investment:remove']"
                type="primary"
                size="mini"
                plain
                :disabled="checkDelBtn(scope)"
                @click="deleteHandle(scope)"
              >{{ $t('删除') }}</el-button>
            </el-tooltip>
            <el-button
              v-hasPermi="['deal:investment:edit']"
              type="primary"
              size="mini"
              plain
              @click="openHoldingChange(scope)"
            >{{ $t('持股变化') }}</el-button>
          </div>
        </div>
        <el-descriptions class="margin-top list-context" title="" :column="3">
          <el-descriptions-item :label="$t('预计投资时间')">
            <span>{{ parseTime(scope.dateInvestmentEstimate) || '-' }}</span>
          </el-descriptions-item>
          <el-descriptions-item :label="$t('投资协议签署时间')">
            {{ parseTime(scope.investmentSignTime) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('首次付款时间')">
            {{ parseTime(scope.dateInvestmentFact) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('预计投资金额') + $t('金额单位')">
            {{ moneyFormatCustom(scope.amountInvestmentEstimate, scope.currency) }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('协议金额') + $t('金额单位')">
            {{ moneyFormatCustom(scope.agreementMoneyInvest, scope.dealCurrency) }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('实际投资金额') + $t('金额单位')">
            {{ moneyFormatCustom(scope.amountInvestmentFact, scope.dealCurrency) }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('进入时持股比例') + '(%)'">
            {{ scope.initialShareholdingRatio || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('最新持股比例') + '(%)'">
            {{ scope.latestShareholdingRatio || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('最新持股数量') + $t('(万股)')">
            {{ moneyFormatCustom(scope.numberOfShares, undefined) }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('已回款总金额') + $t('金额单位')">
            {{ moneyFormatCustom(scope.collectionAmount, scope.dealCurrency) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('已回款本金') + $t('金额单位')">
            {{ moneyFormatCustom(scope.principalRepayment, scope.dealCurrency) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('已回款收益') + $t('金额单位')">
            {{ moneyFormatCustom(scope.collectionIncome, scope.dealCurrency) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('IRR') + '(%)'">
            {{ scope.latestIrr || '-' }}
          </el-descriptions-item>
          <!--<el-descriptions-item :label="$t('MOIC')">-->
          <!--  - -->
          <!--</el-descriptions-item>-->
          <!--<el-descriptions-item :label="$t('DP')">-->
          <!--  - -->
          <!--</el-descriptions-item>-->
          <el-descriptions-item :label="$t('最新估值时间')">
            {{ parseTime(scope.valuaionAccountedDate) || '-' }}
          </el-descriptions-item>
          <el-descriptions-item :label="$t('持有部分估值') + $t('金额单位')">
            {{ moneyFormatCustom(scope.valuationAccounted, scope.dealCurrency) || '-' }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    <el-empty v-else v-loading="loading" :description="$t('暂无数据')" />

    <!-- 添加或修改投资主体对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :destroy-on-close="true"
      append-to-body
      custom-class="dialogForm"
    >
      <dealInvestmentForm ref="dealInvestmentForm" />
    </el-dialog>

    <el-dialog
      :title="$t('持股变化')"
      :visible.sync="holdingsChangeOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      :destroy-on-close="true"
      append-to-body
      custom-class="dialogForm"
    >
      <dealHoldingsChangeList ref="dealHoldingsChangeList" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealInvestment, delDealInvestment } from '@/api/deal/investment/dealInvestment'
import dealInvestmentForm from './dealInvestmentForm.vue'
import dealHoldingsChangeList from '@/views/deal/holdingschange/dealHoldingsChangeList'

export default {
  components: {
    dealHoldingsChangeList,
    dealInvestmentForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 投资主体表格数据
      dealInvestmentList: [],
      // 实际投资时间 参数
      dateInvestmentFacts: [],
      // 弹出层标题
      title: '投资主体',
      // 是否显示弹出层
      formOpen: false,
      // 主体类型字典
      investorTypeOptions: [],
      // 币种字典
      currencyOptions: [],
      // 跟投领头
      dealInvestmentRoleOptions: [],
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        investorId: undefined,
        investorType: undefined,
        currency: undefined,
        dateInvestmentEstimate: undefined,
        dateInvestmentFact: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      holdingsChangeOpen: false
    }
  },
  created() {
    this.getList()
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getDicts('deal_investment_type').then(response => {
      this.investorTypeOptions = response.data
    })
    this.getDicts('deal_investment_role').then(response => {
      this.dealInvestmentRoleOptions = response.data
    })
  },
  methods: {
    /** 查询投资主体列表 */
    getList() {
      this.loading = true
      if (this.dateInvestmentFacts != null && this.dateInvestmentFacts.length > 1) {
        this.queryParams.startDate = this.dateInvestmentFacts[0]
        this.queryParams.endDate = this.dateInvestmentFacts[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listDealInvestment(this.queryParams).then(response => {
        this.dealInvestmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 主体类型字典翻译
    investorTypeFormat(row) {
      return this.selectDictLabel(this.investorTypeOptions, row.investorType)
    },
    roleFormatter(row) {
      return this.selectDictLabel(this.dealInvestmentRoleOptions, row.investmentRole)
    },
    moneyFormatCustom(val, currency) {
      if (this.isEmpty(val)) {
        return '-'
      } else if (currency) {
        return this.moneyFormat(val, 'w') + ' (' + this.currencyFormat({ currency: currency }) + ')'
      } else {
        return this.moneyFormat(val, 'w')
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        investorId: undefined,
        investorType: undefined,
        currency: undefined,
        dateInvestmentEstimate: undefined,
        amountInvestmentEstimate: undefined,
        dateInvestmentFact: undefined,
        amountInvestmentFact: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateInvestmentFacts = []
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealInvestmentForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealInvestmentForm.init(row)
      })
    },
    checkDelBtn(row) {
      var investmentFact = !this.isEmpty(row.amountInvestmentFact) && row.amountInvestmentFact !== 0 && row.amountInvestmentFact !== '0'
      var agreementMoney = !this.isEmpty(row.agreementMoney) && row.agreementMoney !== 0 && row.agreementMoney !== '0'
      return investmentFact || agreementMoney || row.delBtnStatus
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealInvestment(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 持股变化 **/
    openHoldingChange(row) {
      this.holdingsChangeOpen = true
      this.$nextTick(() => {
        this.$refs.dealHoldingsChangeList.queryParams.investorId = row.id
        this.$refs.dealHoldingsChangeList.getList()
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/investment/dealInvestment/export', this.queryParams)
    }
  }
}
</script>
<style scoped lang="scss">
.list-main {
  background-color: #f6f8fd !important;
  padding: 5px 15px;
  margin: 10px 0 15px 0;
  border-radius: 4px;
  box-shadow: 0 1px 1px 1px #f2f2f2;
  cursor: default;

  .list-title-invest {
    height: 40px;
    display: flex;
    justify-content: left;
    align-items: center;
    border-bottom: 1px solid #efeded;

    .list-title-invest-text {
      font-weight: bold;
      font-size: 14px;
      height: 28px;
      display: flex;
      justify-content: left;
      align-items: center;
      width: 80%;

      .list-title-invest-name {
        margin-right: 20px;
      }
    }
  }
  .list-context {
    ::v-deep.el-descriptions__body {
      background-color: #f6f8fd !important;
      padding: 15px 0 0 0;
    }
  }
}

::v-deep .el-descriptions-item__label:not(.is-bordered-label) {
  color: #666666 !important;
}
</style>
